import 'dart:io';

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/style/image_style.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AttachFileForm extends StatelessWidget {
  final List<String> files;
  final VoidCallback onAdd;
  final ValueChanged<String> onSelectFile;
  final List<OcrAttachmentEntity> attachments;
  final ValueChanged<OcrAttachmentEntity> onSelectAttachment;

  const AttachFileForm(
      {Key key,
      this.files,
      this.onAdd,
      this.attachments,
      this.onSelectFile,
      this.onSelectAttachment})
      : super(key: key);

  int get attachmentLength => attachments != null ? attachments.length : 0;

  @override
  Widget build(BuildContext context) {
    bool isEnable = FormEditStatusInheritedWidget.ofEnable(context);
    if (isEnable)
      return GridView.builder(
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            mainAxisSpacing: getDp(14),
            crossAxisSpacing: getDp(14)),
        itemBuilder: _buildItems,
        itemCount: files.length + attachmentLength + 1,
      );
    else
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            S.current.attachments,
            style: TextStyle(
              color: aceDarkGrayColor,
              fontSize: getSp(14),
              fontWeight: FontWeight.w400,
              height: 1,
            ),
          ),
          if (files.length != 0 || attachmentLength != 0)
            GridView.builder(
              padding: EdgeInsets.only(
                top: getDp(10),
              ),
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  mainAxisSpacing: getDp(14),
                  crossAxisSpacing: getDp(14)),
              itemBuilder: _buildNetworkItems,
              itemCount: attachmentLength,
            )
        ],
      );
  }

  Widget _buildItems(BuildContext context, int index) {
    if (index == 0) {
      return _buildAttachAdd();
    } else {
      if (attachmentLength != 0) {
        int _index = index - 1;
        if (_index < attachmentLength) {
          return _buildNetworkItems(context, _index);
        } else {
          _index = _index - attachmentLength;
          return _buildFileItem(files[_index]);
        }
      } else {
        int _index = index - 1;
        return _buildFileItem(files[_index]);
      }
    }
  }

  Widget _buildAttachAdd() {
    return PressedOpacityButton(
      onPressed: onAdd,
      color: aceDarkGrayColor05,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            CupertinoIcons.link,
            size: getDp(27),
            color: aceBlueColor,
          ),
          SizedBox(
            height: getDp(12),
          ),
          Text(
            S.current.attach,
            style: TextStyle(
              color: aceBlueColor,
              fontSize: getSp(18),
              fontWeight: FontWeight.w700,
              height: 14 / 18,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildFileItem(String file) {
    return PressedOpacityButton(
      onPressed: () => onSelectFile(file),
      child: Material(
          clipBehavior: Clip.hardEdge,
          borderRadius: BorderRadius.circular(getDp(5)),
          child: Image.file(
            File(file),
            fit: BoxFit.cover,
          )),
    );
  }

  Widget _buildNetworkItems(BuildContext context, int index) {
    final attach = attachments[index];

    return PressedOpacityButton(
      onPressed: () => onSelectAttachment(attach),
      child: Material(
          clipBehavior: Clip.hardEdge,
          borderRadius: BorderRadius.circular(getDp(5)),
          child: Image.network(
            attach.thumbnail,
            fit: BoxFit.cover,
            frameBuilder: ImageStyle.fadeInFrame,
          )),
    );
  }
}
